package com.weixin.paleituo.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.weixin.paleituo.R;
import com.weixin.paleituo.adapter.TemplateAlbumAdapter;
import com.weixin.paleituo.adapter.TemplateRecyclerAdapter;
import com.weixin.paleituo.base.BaseFragment;
import com.weixin.paleituo.bean.PtListBean;
import com.weixin.paleituo.bean.StatusCode;
import com.weixin.paleituo.bean.TemplateDetailsBean;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.http.HttpUtil;
import com.weixin.paleituo.http.ProgressSubscriber;
import com.weixin.paleituo.rxjava.ApiUtils;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.GridleDecoration;
import com.weixin.paleituo.view.HeaderAndFooterWrapper;
import com.weixin.paleituo.view.SharedMoreTemplateItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * 样板间详情，现场实景
 *
 * @author fengzhen
 * @version v1.0, 2018/4/20
 */
public class TemplateAlbumFragment extends BaseFragment {

    @BindView(R.id.recycler_album_fragment_template_album)
    RecyclerView mRecyclerAlbumFragmentTemplateAlbum;
//    @BindView(R.id.recycler_more_fragment_template_album)
    RecyclerView mRecyclerMoreFragmentTemplateAlbum;
//    @BindView(R.id.linear_more_fragment_template_album)
    LinearLayout mLinearMore;

    private TemplateDetailsBean mTemplate;

    private List<PtListBean> mMoreData = new ArrayList<>();
    private TemplateRecyclerAdapter mMoreAdapter;

    public static TemplateAlbumFragment newInstance(TemplateDetailsBean template) {
        Bundle args = new Bundle();
        TemplateAlbumFragment fragment = new TemplateAlbumFragment();
        args.putSerializable(Contans.INTENT_DATA, template);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View initView(Context context) {
        return UIUtils.inflate(getContext(), R.layout.fragment_template_album);
    }

    @Override
    protected void initData() {
        super.initData();

        mTemplate = (TemplateDetailsBean) getArguments().getSerializable(Contans.INTENT_DATA);

        View footer = UIUtils.inflate(getContext(), R.layout.layout_footer_template);
        mRecyclerMoreFragmentTemplateAlbum = footer.findViewById(R.id.recycler_more_fragment_template_album);
        mLinearMore = footer.findViewById(R.id.linear_more_fragment_template_album);
        mRecyclerMoreFragmentTemplateAlbum.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
//        mRecyclerMoreFragmentTemplateAlbum.addItemDecoration(new SharedMoreTemplateItemDecoration(getContext()));
//        mRecyclerMoreFragmentTemplateAlbum.addItemDecoration(new GridleDecoration(getContext(), false));
        mMoreAdapter = new TemplateRecyclerAdapter(mMoreData, false);
        mRecyclerMoreFragmentTemplateAlbum.setAdapter(mMoreAdapter);

        mRecyclerMoreFragmentTemplateAlbum.setNestedScrollingEnabled(false);
        mRecyclerAlbumFragmentTemplateAlbum.setNestedScrollingEnabled(false);

        mRecyclerAlbumFragmentTemplateAlbum.setLayoutManager(
                new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        TemplateAlbumAdapter albumAdapter = new TemplateAlbumAdapter(mTemplate.getPhotos());
        HeaderAndFooterWrapper<Object> footerWrapper = new HeaderAndFooterWrapper<>(albumAdapter);
        footerWrapper.addFootView(footer);
        mRecyclerAlbumFragmentTemplateAlbum.setAdapter(footerWrapper);

        getMoreTemplate();
    }

    /**
     * 获取更多楼盘数据
     */
    private void getMoreTemplate() {
        Observable<StatusCode<List<PtListBean>>> observable = ApiUtils.getApi()
                .getMoreTemplate(Contans.defaultToken, mTemplate.getId(), mTemplate.getHouseId());
        HttpUtil.getInstance().toSubscribe(observable,
                new ProgressSubscriber<List<PtListBean>>(getContext()) {
                    @Override
                    protected void _onNext(StatusCode<List<PtListBean>> statusCode) {
                        List<PtListBean> data = statusCode.getData();
                        if (data == null || data.size() <= 0) {
                            mLinearMore.setVisibility(View.GONE);
                        } else {
                            mLinearMore.setVisibility(View.VISIBLE);
                            mMoreData.clear();
                            mMoreData.addAll(data);
                            mMoreAdapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    protected void _onError(String message) {
                    }
                }, "", lifecycleSubject, false, true);
    }
}
